package com.order.admin.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.order.admin.bean.Menu;
import com.order.admin.bean.MenusOrder;
import com.order.admin.bean.Order;
import com.order.admin.bean.OrderItem;
import com.order.admin.commonResponse.ComResponse;
import com.order.admin.mapper.MenusOrderMapper;
import com.order.admin.mapper.OrderMapper;
import com.order.admin.service.impl.MenusOrderServiceimpl;
import com.order.admin.service.impl.OrderServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class orderController {
    @Autowired
    MenusOrderServiceimpl menusOrderServiceimpl;

    @Autowired
    MenusOrderMapper menusOrderMapper;

    @Autowired
    OrderServiceimpl orderServiceimpl;

    @Autowired
    OrderMapper orderMapper;

    @ResponseBody
    @GetMapping("/getOrderItem")
    public ComResponse insertMenusOrder(@RequestParam("O_id") String id){
        ComResponse<Object> objectComResponse = new ComResponse<>();
        List<OrderItem> orderItems =  orderMapper.getOrderItem(id);
        Map<String,Object> map = new HashMap<>();
        map.put("orderItems",orderItems);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>();
        queryWrapper.eq("O_id",id);
        String table = orderMapper.selectOne(queryWrapper).getT_id();
        String time = orderMapper.selectOne(queryWrapper).getTime();
        Integer totalPrice = orderMapper.selectOne(queryWrapper).getPrice();
        map.put("table",table);
        map.put("time",time);
        map.put("totalPrice",totalPrice);
        objectComResponse.setData(map);
        return objectComResponse;
    }

    @ResponseBody
    @GetMapping("/getOrderNumber")
    public ComResponse insertMenusOrder(){
        ComResponse<Object> objectComResponse = new ComResponse<>();
        Integer number = orderServiceimpl.count();
        objectComResponse.setData(number);
        return objectComResponse;
    }

    @ResponseBody
    @PostMapping("/updateOrder")
    public ComResponse updateOrder(@RequestBody cn.hutool.json.JSONObject jsonObject){
        Object bodyData = jsonObject.get("bodyData");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>();
        queryWrapper.eq("O_id",JSONUtil.parseObj(bodyData).get("O_id"));
        Order order = orderMapper.selectOne(queryWrapper);
        if(order == null){
            Order insertOrder = new Order();
            insertOrder.setPrice(Integer.parseInt(JSONUtil.parseObj(bodyData).get("totalPrice").toString()));
            insertOrder.setCz_id("c02");
            insertOrder.setO_id(JSONUtil.parseObj(bodyData).get("O_id").toString());
            insertOrder.setT_id("t001");
            insertOrder.setT_state("0");
            Calendar calendar= Calendar.getInstance();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            insertOrder.setTime(calendar.getTime().toString());
        }else{
            order.setPrice(order.getPrice()+Integer.parseInt(JSONUtil.parseObj(bodyData).get("totalPrice").toString()));
            orderServiceimpl.updateById(order);
        }
        JSONArray M_id = JSONUtil.parseObj(bodyData).getJSONArray("M_id");
        JSONArray M_number = JSONUtil.parseObj(bodyData).getJSONArray("M_number");
        for(int i =0;i<M_id.size();i++){
            menusOrderServiceimpl.addMenu(JSONUtil.parseObj(bodyData).get("O_id").toString(),M_id.get(i).toString(),M_number.get(i).toString());
        }
        return null;
    }
}
